package com.sun.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RedisTemplateHash {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/hash")
    public String hashTest(){
        // 新增hashMap值
        redisTemplate.opsForHash().put("hash", "a", "第一个");

        // 获取指定map键是否有值，有就返回，没有返回null
        System.out.println(redisTemplate.opsForHash().get("hash", "a"));

        // 获取指定key中所有键值对
        redisTemplate.opsForHash().put("hash", "b", "第二个");
        redisTemplate.opsForHash().put("hash", "c", "第三个");
        System.out.println(redisTemplate.opsForHash().entries("hash"));

        // 以Map集合的方式添加键值对
        Map map = new HashMap<String, String>();
        map.put("d", "第四个");
        map.put("e", "第五个");
        redisTemplate.opsForHash().putAll("hash", map);
        System.out.println(redisTemplate.opsForHash().entries("hash"));

        // 仅当hashKey不存在时才设置
        redisTemplate.opsForHash().putIfAbsent("hash", "a", "不是第一个了");
        redisTemplate.opsForHash().putIfAbsent("hash", "f", "第六个");
        System.out.println(redisTemplate.opsForHash().entries("hash"));

        // 删除一个或多个hash表字段
        redisTemplate.opsForHash().delete("hash", "e", "f");
        System.out.println(redisTemplate.opsForHash().entries("hash"));

        // 查看hash表中指定字段是否存在
        System.out.println(redisTemplate.opsForHash().hasKey("hash", "f"));

        // 给hash表中指定字段的整数值加上增量increment
        redisTemplate.opsForHash().put("hash", "e", 10);
        redisTemplate.opsForHash().increment("hash", "e", 10);
        System.out.println(redisTemplate.opsForHash().get("hash", "e"));

        // 给hash表中指定字段的浮点值加上increment
//        redisTemplate.opsForHash().put("hash", "f", 15.5);
//        redisTemplate.opsForHash().increment("hash", "f", 21.1);
//        System.out.println(redisTemplate.opsForHash().get("hash", "f"));

        // 获取hash表中所有字段
        System.out.println(redisTemplate.opsForHash().keys("hash"));

        // 获取hash表中字段数量
        System.out.println(redisTemplate.opsForHash().size("hash"));

        // 获取hash表中存在的所有值
        System.out.println(redisTemplate.opsForHash().values("hash"));

        // 匹配获取键值对
        try {
            Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan("hash", ScanOptions.NONE);
            while (cursor.hasNext()) {
                Map.Entry<Object, Object> entry = cursor.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + ": " + value);
            }
            cursor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
